<?php
/**
 * 通知模型类
 * @author tangbl
 *
 */
class TipModel extends Model {
	
	/**
	 * 获取订单详情
	 * @param 订单主键编号 $id
	 */
	public function getOrderDetail($id,$detail=1){
		
		$map['id|orderno'] = "$id";
		$model = D("Order");
		$rows  = $model->getOrderDetail($map,$detail);
		
		$delivery = D("OrderDeliveryUserView");
		$outlet   = D("OrderOutletView");
		
		$m['oldid|orderno'] = "$id";
		$rows['outlet']     = $outlet->where($m)->find();
		$rows['delivery']   = $delivery->where($m)->find();
		
		return $rows;
	}
	
	/**
	 * 获取会员详情数组
	 * @param 主键编号 $id
	 */
	public function getMemberDetail($id){
	
		return M("Member")->find($id);
	}
	
	/**
	 * @deprecated 废弃的方法
	 * 通知注册用户
	 * @param 会员数组   $user
	 * @param 短信标识   $sign
	 */
	public function tipreg($list,$sign){
		
		$email   = $list["email"];
		$mobile  = $list["mobile"];
		
		$tipstu  = trim(get_syscfig_key("tipreg"));
		$eflag   = false;
		$sflag   = false;
		
		if($tipstu == "1"){
			$eflag   = true;
			$sflag   = true;
		}elseif($tipstu == "2"){
			$sflag   = true;
		}elseif($tipstu == "3"){
			$eflag   = true;
		}else{
			return array(2,"failure");
		}
		
		if(isemail($email) && $eflag){
			
			$etemp   = $this->getEmailTemplate($sign);
			$mail    = createMail();
			$content = htmlspecialchars_decode($etemp['content']);
			$einfo   = sendMail($mail,$email,$etemp['title'],$content);
		}		
		
		if(ismobile($mobile) && $sflag){
			
			$stemp   = $this->getSmsTemplate($sign);
			$phone   = createMobile();
			$sinfo   = sendSms($phone,$mobile,$stemp);
		}
		
		$info = array(1,"success");
		return $info;
	}
	
	/**
	 * @deprecated 废弃的方法
	 * 通知注册用户
	 * @param 订单数组   $user
	 * @param 短信标识   $sign
	 */
	public function tipord($list,$sign){
	
		$email   = $list["email"];
		$mobile  = $list["mobile"];
		
		$tipstu  = trim(get_syscfig_key("tipord"));
		$eflag   = false;
		$sflag   = false;
		
		if($tipstu == "1"){
			$eflag   = true;
			$sflag   = true;
		}elseif($tipstu == "2"){
			$sflag   = true;
		}elseif($tipstu == "3"){
			$eflag   = true;
		}else{
			return array(2,"failure");
		}
		
		if(isemail($email) && $eflag){
			
			$etemp   = $this->getEmailTemplate($sign);
			$mail    = createMail();
			$content = htmlspecialchars_decode($etemp['content']);
			$einfo   = sendMail($mail,$email,$etemp['title'],$content);
		}		
		
		if(ismobile($mobile) && $sflag){
			
			$stemp   = $this->getSmsTemplate($sign);
			$phone   = createMobile();
			$sinfo   = sendSms($phone,$mobile,$stemp);
		}
		
		$info = array(1,"success");
		return $info;
	}
	
	/**
	 * @deprecated 废弃的方法
	 * 通知订单状态
	 * @param 订单数组   $user
	 * @param 模板标识   $sign
	 */
	public function tipstu($list,$sign,$now){
	
		$tipstu  = trim(get_syscfig_key("tipstu"));
		$eflag   = false;
		$sflag   = false;
		
		if($tipstu == "1"){
			$eflag   = true;
			$sflag   = true;
		}elseif($tipstu == "2"){
			$sflag   = true;
		}elseif($tipstu == "3"){
			$eflag   = true;
		}else{
			return array(2,"failure");
		}
		
		$email   = $list["email"];
		$mobile  = $list["mobile"];
		
		$einfo   = array(1,"");
		$sinfo   = array(1,"");
		
		if(isemail($email) && $eflag){
			
			$etemp   = $this->getEmailTemplate($sign);
			$now->assign("detail",$list);
			$content = $now->fetch("",$etemp['content']);
		
			$mail    = createMail();
			$content = htmlspecialchars_decode($etemp['content']);
			$einfo   = sendMail($mail,$email,$etemp['title'],$content);
		}
	
		if(ismobile($mobile) && $sflag){
				
			$stemp   = $this->getSmsTemplate($sign);
			$now->assign("detail",$list);
			$content = $now->fetch("",$stemp);
			
			$phone   = createMobile();
			$sinfo   = sendSms($phone,$mobile,$content);
				
			$rows['content'] = $content;
			$rows['email']   = $mobile;
			$rows['status']  = $sinfo[0];
			$rows['feed']    = $sinfo[1];
			$this->smsSendLog($rows);
		}
	
		
		$info = array(1,"success");
		return $info;
	}
	
	/**
	 * 获取短信模板
	 * @paraa 模板编号 $id
	 */
	public function getSmsTemplate($id){
	
		$map['id|sign'] = "$id";
		
		$lang = getLang();
		$rows = M("SmsTemp")->where($map)->getField("content");
		$josn = json_decode($rows,true);
		return $josn[$lang];
	}
	
	/**
	 * 添加短信发送记录
	 */
	public function addSmsSend($map){
	
		$map['lang']    = LANG_SET;
		$map['addtime'] = time();
		//$map['solevar'] = $solevar;
	
		return M("SmsSend")->add($map);
	}
	
	/**
	 * 保存短信发送记录
	 */
	public function saveSmsSend($map){
	
		return M("SmsSend")->save($map);
	}
	
	/**
	 * 添加短信发送记录详情
	 */
	public function smsSendLog($map){
	
		$map['lang']    = LANG_SET;
		$map['addtime'] = time();
		
		return M("SmsSendLog")->add($map);
	}
	
	/**
	 * 验证手机号短信接受数量
	 */
	public function checkSmsNum($mobile){
		
		$system= C("SMS_MOBILE");
		if($mobile == $system){
			$info = array(false,"");
		}else{
			$max   = C("SMS_MEMBER_MAX");
			$model = M("SmsSendLog");
			
			$s = strtotime(date("Y-m-d"));
			$e = $s+86400;
			$where['mobile'] = $mobile;
			$where['status'] = 1;
			$where['addtime']= array(array("egt",$s),array("lt", $e),"and");
			$total = $model->where($where)->count("id");
			
			if($total>=$max){
				$info = array(true,L("sms_send_max"));
			}else{
				$info = array(false,"");
			}
		}
		return $info;
	}
	
	/**
	 * 获取邮件模板
	 * @paraa 模板编号 $id
	 */
	public function getEmailTemplate($id,$lang){
		
		$map['id|sign'] = "$id";
		
		//$lang = getLang();
		$rows = M("EmailTemp")->where($map)->field("title,content")->find();
		$josn = json_decode($rows['content'],true);
		$text = json_decode($rows['title'],true);
		
		return array("title"=>$text[$lang],"content"=>$josn[$lang]);
	}
	
	/**
	 * 添加邮件发送记录
	 */
	public function addEmailSend($map){
	
		$map['lang']    = LANG_SET;
		$map['addtime'] = time();
	
		return M("EmailSend")->add($map);
	}
	
	/**
	 * 添加邮件发送记录
	 */
	public function saveEmailSend($map){
	
		return M("EmailSend")->save($map);
	}
	
	/**
	 * 添加邮件发送记录详情
	 */
	public function emailSendLog($map){
	
		$map['lang']    = LANG_SET;
		$map['addtime'] = time();
	
		return M("EmailSendLog")->add($map);
	}
}
?>